package com.share.two.e_aspect.b_annotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;

@Service
@Aspect
public class MyAspect{
	
	/**
	 * 公共的切入点表达式
	 */
	@Pointcut("execution(* com.share.two.e_aspect.b_annotation.UserServiceImpl.*(..))")
	public void myPointCut(){
		
	}

	/**
	 * @param joinPoint :连接点，也就是目标方法（切入点）的描述
	 */
	//@Before("execution(* com.share.two.e_aspect.b_annotation.UserServiceImpl.*(..))")
	public void myBefore(JoinPoint joinPoint) {
		System.out.println("aspect_annotation:前置通知:" + joinPoint.getSignature().getName());
	}
	
	/**
	 * @param joinPoint:连接点，也就是目标方法（切入点）的描述
	 * @param ret :目标方法的返回值
	 */
	//@AfterReturning(value="myPointCut()", returning="ret")
	public void myAfterReturning(JoinPoint joinPoint, Object ret) {
		System.out.println("aspect_annotation:后置通知:" + joinPoint.getSignature().getName() + "返回值:" + ret);
	}
	
	/**
	 * @param ProceedingJoinPoint : JoinPoint的子类，同样是描述目标方法用的
	 * @return
	 * @throws Throwable
	 */
	@Around(value = "myPointCut()")
	public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("前方法");
		Object obj = joinPoint.proceed();
		System.out.println("后方法:"+ "目标方法：" + joinPoint.getSignature().getName() + "返回值：" + obj);
		return obj;
	}
	
	/**
	 * @param joinPoint:目标方法的描述
	 * @param e ：异常的描述
	 */
	//@AfterThrowing(value = "myPointCut()", throwing = "e")
	public void myAfterThrowing(JoinPoint joinPoint, Throwable e) {
		System.out.println("aspect_annotation：抛异常通知--》" + "目标方法:" + joinPoint.getSignature().getName() + "异常:" + e.getMessage());
	}
	
	/**
	 * @param joinPoint :目标方法的描述
	 */
	//@After(value = "myPointCut()")
	public void myAfter(JoinPoint joinPoint) {
		System.out.println("aspect_annotation：最终通知--》" + "目标方法：" + joinPoint.getSignature().getName());
	}
	
	

}
